package com.hnit.applet.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hnit.applet.entity.BaseDim;
import com.hnit.applet.mapper.BaseDimMapper;
import org.springframework.stereotype.Service;

/**
 * @author zwj
 * @date 2022/7/7
 * @description TODO
 */

@Service
public class BaseDimService extends ServiceImpl<BaseDimMapper, BaseDim> {

    /**
     * 通过ID查询单条数据
     *
     * @param dimId 主键
     *
     * @return 实例对象
     */
    public BaseDim queryById(String dimId) {
        return baseMapper.selectById(dimId);
    }

    /**
     * 分页查询
     *
     * @param baseDim dim
     * @param page    分页
     *
     */
    public IPage<BaseDim> paginQuery(BaseDim baseDim, Page<BaseDim> page) {
        //1. 构建动态查询条件
        LambdaQueryWrapper<BaseDim> queryWrapper = new LambdaQueryWrapper<>();

        if (StrUtil.isNotBlank(baseDim.getPropCode())) {
            queryWrapper.eq(BaseDim::getPropCode, baseDim.getPropCode());
        }
        if (StrUtil.isNotBlank(baseDim.getPropName())) {
            queryWrapper.eq(BaseDim::getPropName, baseDim.getPropName());
        }
        if (StrUtil.isNotBlank(baseDim.getPDimId())) {
            queryWrapper.eq(BaseDim::getPDimId, baseDim.getPDimId());
        }

        if (StrUtil.isNotBlank(baseDim.getDescription())) {
            queryWrapper.eq(BaseDim::getDescription, baseDim.getDescription());
        }
        if (StrUtil.isNotBlank(baseDim.getOrgName())) {
            queryWrapper.eq(BaseDim::getOrgName, baseDim.getOrgName());
        }
        if (StrUtil.isNotBlank(baseDim.getOrgCode())) {
            queryWrapper.eq(BaseDim::getOrgCode, baseDim.getOrgCode());
        }
        if (StrUtil.isNotBlank(baseDim.getLevel())) {
            queryWrapper.eq(BaseDim::getLevel, baseDim.getLevel());
        }
        if (StrUtil.isNotBlank(baseDim.getPOrgCode())) {
            queryWrapper.eq(BaseDim::getPOrgCode, baseDim.getPOrgCode());
        }
        if (StrUtil.isNotBlank(baseDim.getEnableApplets())) {
            queryWrapper.eq(BaseDim::getEnableApplets, baseDim.getEnableApplets());
        }
        if (StrUtil.isNotBlank(baseDim.getSocialCode())) {
            queryWrapper.eq(BaseDim::getSocialCode, baseDim.getSocialCode());
        }
        return baseMapper.selectByPage(page, queryWrapper);
    }

    /**
     * 新增数据
     *
     * @param baseDim 实例对象
     *
     * @return 实例对象
     */
    public BaseDim insert(BaseDim baseDim) {
        baseMapper.insert(baseDim);
        return baseDim;
    }

    /**
     * 更新数据
     *
     * @param baseDim 实例对象
     *
     * @return 实例对象
     */
    public BaseDim update(BaseDim baseDim) {
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<BaseDim> chainWrapper = new LambdaUpdateChainWrapper<>(baseMapper);

        if (StrUtil.isNotBlank(baseDim.getPropCode())) {
            chainWrapper.eq(BaseDim::getPropCode, baseDim.getPropCode());
        }
        if (StrUtil.isNotBlank(baseDim.getPropName())) {
            chainWrapper.eq(BaseDim::getPropName, baseDim.getPropName());
        }
        if (StrUtil.isNotBlank(baseDim.getPDimId())) {
            chainWrapper.eq(BaseDim::getPDimId, baseDim.getPDimId());
        }

        if (StrUtil.isNotBlank(baseDim.getDescription())) {
            chainWrapper.eq(BaseDim::getDescription, baseDim.getDescription());
        }
        if (StrUtil.isNotBlank(baseDim.getOrgName())) {
            chainWrapper.eq(BaseDim::getOrgName, baseDim.getOrgName());
        }
        if (StrUtil.isNotBlank(baseDim.getOrgCode())) {
            chainWrapper.eq(BaseDim::getOrgCode, baseDim.getOrgCode());
        }
        if (StrUtil.isNotBlank(baseDim.getLevel())) {
            chainWrapper.eq(BaseDim::getLevel, baseDim.getLevel());
        }
        if (StrUtil.isNotBlank(baseDim.getPOrgCode())) {
            chainWrapper.eq(BaseDim::getPOrgCode, baseDim.getPOrgCode());
        }
        if (StrUtil.isNotBlank(baseDim.getEnableApplets())) {
            chainWrapper.eq(BaseDim::getEnableApplets, baseDim.getEnableApplets());
        }
        if (StrUtil.isNotBlank(baseDim.getSocialCode())) {
            chainWrapper.eq(BaseDim::getSocialCode, baseDim.getSocialCode());
        }
        //2. 设置主键，并更新
        chainWrapper.set(BaseDim::getDimId, baseDim.getDimId());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if (ret) {
            return queryById(baseDim.getDimId());
        } else {
            return baseDim;
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param dimId 主键
     *
     * @return 是否成功
     */
    public boolean deleteById(String dimId) {
        int total = baseMapper.deleteById(dimId);
        return total > 0;
    }
}
